#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:taotieren

"""
    rpi.gpio 需要 root 权限，板上运行需要使用
    sudo python gui_slider.py
"""
from tkinter import *

""" python3 中请安装 tkintertoy 来进行程序调试和验证 """
import RPi.GPIO as GPIO

""" 非树莓派环境下可以用 rpi.gpio_def 代替 rpi.gpio 来进行程序调试和验证 """
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 500)
pwm.start(100)


class App:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        scale = Scale(frame, from_=0, to=100,
                      orient=HORIZONTAL,
                      command=self.update)
        scale.grid(row=0)

    def update(self, duty):
        pwm.ChangeDutyCycle(float(duty))


root = Tk()
root.wm_title('PWM Power Control')
app = App(root)
root.geometry("200x50+0+0")
root.mainloop()
